package com.linx.common.web.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.linx.common.core.utils.SpringUtils;
import com.linx.common.web.domain.bo.EventSubscriptionByEventTypesRequest;
import com.linx.common.web.domain.vo.AddAcpsAuthConfigVo;
import com.linx.common.web.domain.vo.ResponseVo;
import com.linx.common.web.properties.HkProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/***
 * @ClassName EventSubscriptionByEventTypesControlUtils
 * @Description 事件订阅控制工具类
 * @Author gushaoliang
 * @Date 2024/12/4 下午5:17
 * @Version 1.0
 */
@Component
@Slf4j
public class EventSubscriptionByEventTypesControlUtils {
    private String agreement;
    private String host;
    private String appKey;
    private String appSecret;
    private String artemisPath;


    public EventSubscriptionByEventTypesControlUtils() {
        HkProperties hkProperties = SpringUtils.getBean(HkProperties.class);
        agreement = hkProperties.getAgreement();
        host = hkProperties.getHost();
        appKey = hkProperties.getAppKey();
        appSecret = hkProperties.getAppSecret();
        artemisPath = hkProperties.getArtemisPath();
    }

    //按事件类型订阅事件
    public  ResponseVo eventSubscriptionByEventTypes(EventSubscriptionByEventTypesRequest eventSubscriptionByEventTypesRequest) throws Exception {
        String apiPath = artemisPath +"/api/eventService/v1/eventSubscriptionByEventTypes";
        Map<String, String> path = new HashMap<>(2);
        path.put(agreement+"://", artemisPath + apiPath);

        String body= JSON.toJSONString(eventSubscriptionByEventTypesRequest);
        String json = ArtemisHttpUtil.doPostStringArtemis( path, body, null,  null, "application/json");

        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json：" + jsonObject);
        ResponseVo responseVo = JSON.parseObject(json,
            new TypeReference<ResponseVo>() {
            });
        return responseVo;
    }
}
